package ui.juego.mapamundi

import dominio.datos.Caracteristica
import dominio.investigacion.Mapamundi
import dominio.ubicaciones.Lugar
import dominio.ubicaciones.Pais
import org.uqbar.arena.bindings.NotNullObservable
import org.uqbar.arena.bindings.PropertyAdapter
import org.uqbar.arena.layout.ColumnLayout
import org.uqbar.arena.layout.VerticalLayout
import org.uqbar.arena.widgets.Button
import org.uqbar.arena.widgets.Label
import org.uqbar.arena.widgets.List
import org.uqbar.arena.widgets.Panel
import org.uqbar.arena.windows.SimpleWindow
import org.uqbar.arena.windows.WindowOwner
import ui.juego.utilidades.IconUtils
import java.awt.Color

class MapamundiWindow extends SimpleWindow<Mapamundi> {
	
	new(WindowOwner owner, Mapamundi model) {
		super(owner, model)
		title = "Mapamundi"
		iconImage = IconUtils.path
	}
	
	override protected addActions(Panel actions) {}
	
	override protected createFormPanel(Panel mainPanel) {} 

	override createMainTemplate(Panel mainPanel) {
		var contentPanel = new Panel(mainPanel) => [
			layout = new ColumnLayout(2)		
		]
		
		var leftPanel = new Panel(contentPanel) => [
			layout = new VerticalLayout
		]
		
		crearPanelListadoPaises(leftPanel)
		crearPanelBotonera(leftPanel)
		crearPanelPaisSeleccionado(contentPanel)
				
	}
	
	
	def crearPanelListadoPaises(Panel owner) {
		var Panel panelListadoPaises = new Panel(owner) => [
			layout = new VerticalLayout
		]	
		
		new Label(panelListadoPaises) => [
			text = "Paises"
			foreground = Color.WHITE
			background = Color.GRAY
		]
		new List<Pais>(panelListadoPaises) => [
			bindItemsToProperty("paises").adapter = new PropertyAdapter(Pais, "nombre")
			heigth = 180
			width = 100
			bindValueToProperty("paisSeleccionado")
		]
		
	}
	
	def crearPanelPaisSeleccionado(Panel owner) {
		var Panel paisSeleccionadoPanel = new Panel(owner) => [
			layout = new VerticalLayout
		]
		
		crearPanelNombrePaisSeleccionado(paisSeleccionadoPanel)
		crearPanelCaracteristicasPaisSeleccionado(paisSeleccionadoPanel)
		crearPanelConexionesPaisSeleccionado(paisSeleccionadoPanel)
		crearPanelLugaresPaisSeleccionado(paisSeleccionadoPanel)		
	}
	
	def crearPanelNombrePaisSeleccionado(Panel owner) {
		var Panel nombrePaisPanel = new Panel(owner) => [
			layout = new VerticalLayout
		]
		
		new Label(nombrePaisPanel).text = "Nombre:"
		new Label(nombrePaisPanel) => [
			bindValueToProperty("paisSeleccionado.nombre")
			fontSize = 14
			width = 200
		]
	}
	
	def crearPanelCaracteristicasPaisSeleccionado(Panel owner) {
		var Panel caracteristicasPaisPanel = new Panel(owner) => [
			layout = new VerticalLayout
		]
		
		new Label(caracteristicasPaisPanel)  => [
			text = "Caracteristicas"
			foreground = Color.WHITE
			background = Color.GRAY
		]
		new List<Caracteristica>(caracteristicasPaisPanel) => [
			bindItemsToProperty("paisSeleccionado.caracteristicas").adapter = new PropertyAdapter(Caracteristica, "dato")
			heigth = 50
			width = 200
		]
	}
	
	def crearPanelConexionesPaisSeleccionado(Panel owner) {
		
		var Panel conexionesPaisPanel = new Panel(owner) => [
			layout = new VerticalLayout
		]
		
		new Label(conexionesPaisPanel)  => [
			text = "Conexiones"
			foreground = Color.WHITE
			background = Color.GRAY
		]
		new List<Pais>(conexionesPaisPanel) => [
			bindItemsToProperty("paisSeleccionado.conexiones").adapter = new PropertyAdapter(Pais, "nombre")
			heigth = 50
			width = 200
		]
	}
	
	def crearPanelLugaresPaisSeleccionado(Panel owner) {
		
		var Panel lugaresPaisPanel = new Panel(owner) => [
			layout = new VerticalLayout
		]
		
		new Label(lugaresPaisPanel) => [
			text = "Lugares de Interes"
			foreground = Color.WHITE
			background = Color.GRAY
		]
		new List<Lugar>(lugaresPaisPanel) => [
			bindItemsToProperty("paisSeleccionado.lugares").adapter = new PropertyAdapter(Lugar, "nombreTipoLugar")
			heigth = 50
			width = 200
		]
	}
	
	def crearPanelBotonera(Panel owner) {
		var Panel botoneraPanel = new Panel(owner) => [
			layout = new VerticalLayout
		]
		
		var eliminar = new Button(botoneraPanel) => [
			caption = "Eliminar"
			width = 100
			onClick[ |
				modelObject.eliminarPaisSeleccionado
			]
		]

		var editar = new Button(botoneraPanel) => [
			caption = "Editar"
			width = 100
			onClick[ |
				var pais = modelObject.paisSeleccionado
				new EdicionPaisExistenteDialog(this, pais).open	
			]
		]

		new Button(botoneraPanel) => [
			caption = "Nuevo"
			width = 100
			onClick[ |
				val pais = modelObject.nuevoPais
				new EdicionPaisNuevoDialog(this, pais).open	
			]
		]

		var elementSelected = new NotNullObservable("paisSeleccionado")
		editar.bindEnabled(elementSelected)
		eliminar.bindEnabled(elementSelected)
	}
	
}